package com.yaozheng.vocationaltraining.service.impl.exam;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IPKGameCategoryView;
import com.yaozheng.vocationaltraining.iview.exam.IPKHallView;
import com.yaozheng.vocationaltraining.service.exam.PKHallService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * Created by moon on 2017/11/15.
 */

@EBean
public class PKHallServiceImpl implements PKHallService {
    IPKHallView iTokenBaseView;

    @Override
    public void init(IPKHallView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;
    }

    @Background
    @Override
    public void list(int gameCateId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/game-cate/"+gameCateId+"/rooms",
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.responseSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.responseError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void seat(final int seatId,final int roomId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/seat/"+seatId+"/sit",
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.seatSuccess(jsonObject,roomId,seatId);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.seatError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .seatError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void quickPartIn(int gameCategoryId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/game-cate/"+gameCategoryId+"/quick-join",
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.quickPartInSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.quickPartInError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .quickPartInError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void compare(int playerId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/player/"+playerId,
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.pkCompareSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.pkCompareError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .pkCompareError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
